package com.nvwa.pagehelper;

import com.github.pagehelper.PageInfo;
import com.nvwa.coding.util.safe.Coding;
import com.nvwa.coding.util.json.JsonUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.stream.Collectors;

/**
 * PageInfoBuilder 类。
 *
 * @author wangjing
 * @date 2024-08-12 [wangjing] 创建。
 */
@Slf4j
public class MemoryPageUtil {
    public static <T> PageInfo<T> pagination(List<T> records, int pageNum, int pageSize) {
        log.info("---M[MemoryPageUtil] pagination params,records.size:{},pageNum:{},pageSize:{}", records.size(), pageNum, pageSize);
        log.debug("---M[MemoryPageUtil] pagination params,records:{}", JsonUtil.toString(records));

        //参数限定
        if (Coding.isEmpty(records)) {
            return new PageInfo<>();
        }
        if (pageNum < 1) {
            pageNum = 1;
        }
        if (pageSize < 1) {
            pageSize = 10;
        }

        //分页参数计算
        int totalCount = records.size();
        int remainder = totalCount % pageSize;
        int pageCount = (remainder > 0) ? totalCount / pageSize + 1 : totalCount / pageSize;
        //一页显示所有
        if (pageSize >= totalCount) {
            pageCount = 1;
        }
        //页码超出
        if (pageNum > pageCount) {
            return buildNoDataPageInfo(totalCount, pageCount, pageNum, pageSize);
        }
        //分页
        List<T> newList = records.stream()
                .skip(((long) (pageNum - 1) * pageSize))
                .limit(pageSize)
                .collect(Collectors.toList());
        //构建分页对象
        PageInfo<T> pageInfo = buildPageInfoAllArgs(newList, totalCount, pageCount, pageNum, pageSize);
        log.debug("---M[MemoryPageUtil] pagination res,pageInfo:{}", JsonUtil.toString(pageInfo));
        return pageInfo;
    }

    private static <T> List<T> pageList(List<T> records, int pageNum, int pageSize) {
        //参数限定
        if (Coding.isEmpty(records)) {
            return Coding.arrayList();
        }
        if (pageNum < 1) {
            pageNum = 1;
        }
        if (pageSize < 1) {
            pageSize = 10;
        }
        //分页参数计算
        int totalCount = records.size();
        int remainder = totalCount % pageSize;
        int pageCount = (remainder > 0) ? totalCount / pageSize + 1 : totalCount / pageSize;
        //一页显示所有
        if (pageSize >= totalCount) {
            pageCount = 1;
        }

        //页码超出
        if (pageNum > pageCount) {
            return Coding.arrayList();
        }

        return records.stream()
                .skip(((long) (pageNum - 1) * pageSize))
                .limit(pageSize)
                .collect(Collectors.toList());
    }

    /**
     * 构建分页对象
     *
     * @param newList  集合
     * @param total    集合总对象数
     * @param pages    分页数量
     * @param pageNum  当前页码
     * @param pageSize 每页数量
     * @return PageInfo
     */
    private static <T> PageInfo<T> buildPageInfoAllArgs(List<T> newList, int total, int pages, int pageNum, int pageSize) {
        PageInfo<T> pageInfo = new PageInfo<>(newList);
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        pageInfo.setPages(pages);
        pageInfo.setTotal(total);
        return pageInfo;
    }

    /**
     * 构建空数据分页对象
     *
     * @param pages    分页页数
     * @param pageNum  当前页码
     * @param pageSize 每页数量
     * @return PageInfo
     */
    private static <T> PageInfo<T> buildNoDataPageInfo(int total, int pages, int pageNum, int pageSize) {
        PageInfo<T> pageInfo = new PageInfo<T>(Coding.arrayList());
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        pageInfo.setPages(pages);
        pageInfo.setTotal(total);
        return pageInfo;
    }
}